package wiki.generic.concretemed;

import java.util.HashMap;
import java.util.List;

import wiki.generic.contents.ContentsPrototypeFactory;
import wiki.generic.io.ContentsReader;
import wiki.generic.io.ContentsWriter;
import wiki.generic.io.Element;
import wiki.generic.io.ElementsMapReader;
import wiki.generic.io.ElementsMapWriter;
import wiki.generic.io.Page;
import wiki.generic.io.PathKeeper;
import wiki.generic.managers.PagesMapManager;
import wiki.generic.med.EditingService;
import wiki.generic.utilities.AlreadyExistingElementException;
import wiki.generic.utilities.PageNotFoundException;

public class EditingServiceMediator implements EditingService {

	private ContentsWriter cwriter;
	private ContentsReader creader;
	private ContentsPrototypeFactory pfactory;
	private ElementsMapReader elreader;
	private ElementsMapWriter elwriter;
	private PathKeeper elmap;

	public EditingServiceMediator(ContentsWriter cwriter, ElementsMapReader elreader, PathKeeper elmap, ElementsMapWriter elwriter) {
		super();
		this.cwriter = cwriter;
		this.elreader=elreader;
		this.elmap=elmap;
		this.elwriter=elwriter;
	}

		
	public void setCreader(ContentsReader creader) {
		this.creader = creader;
	}

	
	public int insertElement(String corso,String type, String text) {
		String sharePath=elmap.getPathMap().get(type);
		HashMap<Integer, Element> map=elreader.getElementsMap(sharePath);
		int ID=map.size()+1;
		map.put(ID, new Element(text, corso));
		elwriter.writeElementsMap(map, sharePath);
		return ID;
	}

	@Override
	public void setPFactory(ContentsPrototypeFactory pfactory) {
		this.pfactory=pfactory;
	}
	
	
	public void contentDown(String corso,String current,String name) throws PageNotFoundException{
		String path=PagesMapManager.getPmanager().getPagesMap().get(corso).getValueById(current);
		Page p=creader.getPage(path);
		p.down(name);
		cwriter.writeContents(p.getContentsList(), path);
	}
	public void appendContent(String corso,String current,String name,String type, int ID,List<String> attributes) throws AlreadyExistingElementException, PageNotFoundException{
		String path=PagesMapManager.getPmanager().getPagesMap().get(corso).getValueById(current);
		Page p=creader.getPage(path);
		p.addContent(pfactory.generateContent(name, type, ID, attributes));
		cwriter.writeContents(p.getContentsList(),path );
	}
	
	public void deleteContent(String corso,String current, String name) throws PageNotFoundException{
		String path=PagesMapManager.getPmanager().getPagesMap().get(corso).getValueById(current);
		Page p=creader.getPage(path);
		p.deleteContent(name);
		cwriter.writeContents(p.getContentsList(), path);
	}
	
	public void modifyContent(String corso,String current, String name, int ID, List<String> attributes) throws PageNotFoundException{
		String path=PagesMapManager.getPmanager().getPagesMap().get(corso).getValueById(current);
		Page p=creader.getPage(path);
		p.modifyContent(name, ID, attributes);
		cwriter.writeContents(p.getContentsList(), path);
	}
	
	
	
	public void modifyIDElement(String corso,String current, String name, String type, int ID, String newPar, List<String> attributes) throws PageNotFoundException{
		String path=elmap.getPathMap().get(type);
		HashMap<Integer, Element> map=elreader.getElementsMap(path);
		if(map.get(ID).getInfo().compareTo(newPar)!=0 && map.get(ID).getOwner().compareTo(corso)==0){
			map.remove(ID);
			map.put(ID, new Element(newPar, corso));
			elwriter.writeElementsMap(map, path);
		}
		modifyContent(corso,current, name, ID, attributes);
	}
	

	
	
	public void contentUp(String corso,String current,String name) throws PageNotFoundException{
		String path=PagesMapManager.getPmanager().getPagesMap().get(corso).getValueById(current);
		Page p=creader.getPage(path);
		p.up(name);
		cwriter.writeContents(p.getContentsList(), path);
	}
	

	
	
}
